//
//  KKBusinessAlertView.swift
//  KKBusiness_iOS
//
//  Created by 刘树华 on 2017/5/26.
//  Copyright © 2017年 刘树华. All rights reserved.
//

import Foundation
import UIKit
import SnapKit

protocol BusinessAlertDelegate : AnyObject {
    func alertViewDidSelectWithIndex(_ alertView:BusinessAlert,index:NSInteger)
}

class BusinessAlert: UIView {
//   MARK 定制化参数
    private let alertWidthPercent : CGFloat = 0.8
    private let alertDeafaultHeight : CGFloat = 150
    private let titleverticalPadding : CGFloat = 10
    private let titlehorizontalPadding : CGFloat = 10
    private let messageVerticalPadding : CGFloat = 10
    private let messageHorizonPadding : CGFloat = 10
    private let screenWidth = UIScreen.main.bounds.width
    private let screenHeight = UIScreen.main.bounds.height
    private var btnArr = [String]()
    weak var businessDelegate : BusinessAlertDelegate?
    init(delegate:BusinessAlertDelegate,title:String?=nil,message:String,buttons:String...)  {
       
       super.init(frame: UIScreen.main.bounds)
        self.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.3)
        btnArr.removeAll()
        businessDelegate = delegate
        
        for str in buttons {
            btnArr.append(str)
        }
        var titleSize : CGSize?
        var messageSize : CGSize?
        let alertWidth = screenWidth*alertWidthPercent
        
        if title != nil{
            let nstitle = NSString(string : title!)
           titleSize = nstitle.boundingRect(with:  CGSize(width:  alertWidth-titlehorizontalPadding*2, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font:UIFont.boldSystemFont(ofSize: 17)], context: nil).size
        }
        let nsmessage = NSString(string: message)
        
        messageSize = nsmessage.boundingRect(with: CGSize(width: alertWidth-messageHorizonPadding*2, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 17)], context: nil).size
        
        var alertHeight : CGFloat = 0
        
        
        if titleSize != nil {
            alertHeight += titleSize!.height+titleverticalPadding
        }
        if messageSize != nil {
            alertHeight += messageSize!.height + messageVerticalPadding*2
        }
        alertHeight += 47
        let alertView = UIView()
        alertView.backgroundColor = UIColor.red
        alertView.center = self.center
        alertView.bounds = CGRect(x: 0, y: 0, width: alertWidth, height: alertHeight)
        self.addSubview(alertView)
        
        var titleLabel : UILabel?
        
        if titleSize != nil{
            titleLabel = UILabel()
            titleLabel?.backgroundColor = UIColor.clear
            titleLabel?.textAlignment = .center
            titleLabel?.text = title
            
            titleLabel?.frame = CGRect(x: titlehorizontalPadding, y: titleverticalPadding, width: alertWidth - titlehorizontalPadding*2, height: titleSize!.height)
            titleLabel?.textColor = UIColor.black
            alertView.addSubview(titleLabel!)
        }
        
        
        let messageLabel = UILabel()
        messageLabel.backgroundColor = UIColor.clear
        messageLabel.textAlignment = .center

        messageLabel.numberOfLines = 0
        messageLabel.text = message
        messageLabel.font = UIFont.systemFont(ofSize: 17)
       messageLabel.textColor = UIColor.black
        if titleLabel != nil{
            
          messageLabel.frame = CGRect(x: messageHorizonPadding, y: titleLabel!.frame.maxY + messageVerticalPadding, width: alertWidth - messageHorizonPadding*2, height: messageSize!.height)
        }else{
             messageLabel.frame = CGRect(x: messageHorizonPadding, y: 20 + messageVerticalPadding, width: alertWidth - messageHorizonPadding*2, height: messageSize!.height)
        }
        
        alertView.addSubview(messageLabel)

        var width:CGFloat = 0
        
        if btnArr.count > 1 {
            width = alertWidth/2
        }else{
          width =  alertWidth/CGFloat(btnArr.count)
        }
        for (i,str) in btnArr.enumerated(){
            if i > 1 {
                break
            }
            let btn = UIButton()
            btn.setTitle(str, for: .normal)
            btn.tag = i+10
            btn.setTitleColor(UIColor.blue, for: .normal)
            btn.addTarget(self, action: #selector(BusinessAlert.alertViewBtnClick(btn:)), for: .touchUpInside)
            btn.backgroundColor = UIColor.yellow
            btn.addLine(type: .top, width: 0.5, color: UIColor.black, layout: nil)
            if btnArr.count > 1 {
                if i == 0 {
                    btn.addLine(type: .right, width: 0.5, color: UIColor.black, layout: nil)
                }
            }
            
            btn.frame = CGRect(x: CGFloat(i)*width, y:  messageLabel.frame.maxY+messageVerticalPadding, width: width, height: 47)
            alertView.addSubview(btn)
        }
}
    
    func dismiss ()  {
        
        
        UIView.animate(withDuration: 0.1) { 
           self.removeFromSuperview() 
        }
       
    }
    

   @objc fileprivate func alertViewBtnClick(btn:UIButton)  {
        dismiss()
        
        businessDelegate?.alertViewDidSelectWithIndex(self, index: btn.tag-10)
        
    }
    
    
    func show () {
        
        UIView.animate(withDuration: 0.1) {
            UIApplication.shared.delegate?.window??.addSubview(self)
        }
        
        
    }
  
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
}
